package com.david;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@SpringBootApplication
public class MyGateway1Application {
    public static void main(String[] args) {
        SpringApplication.run(MyGateway1Application.class,args);
    }


    @Bean
    public RouteLocator routeLocator (RouteLocatorBuilder locatorBuilder) {

        return locatorBuilder.routes()
                .route(p ->
                        p.path("/xxoo")
                                .filters(f -> f.stripPrefix(1))
                                .uri("http://mashibing.com")
                )

                .route(p ->

                        p.path("/go")
                                .filters(f -> f.stripPrefix(1))
                                .uri("lb://MDB")
                )

                .build();
    }


//    @Bean
//    public RouterFunction<ServerResponse> routerFunction(FluxHandler handler){
//        RouterFunction<ServerResponse> route = RouterFunctions.route(
//                RequestPredicates
//                        .path("/003")
//                , req -> ServerResponse.ok().body(BodyInserters.fromValue("xxoo")))
//                //匹配多条
//                .andRoute(RequestPredicates.GET("/002"),handler::getXXX)
//                .andRoute(RequestPredicates.GET("/003"),handler::getJsonXXX)
//                .andRoute(RequestPredicates.GET("/004"),handler::getParams)
//                .andRoute(RequestPredicates.GET("/005/{id}/{name}"),handler::getPath)//匹配的时候格式需要一致
//        ;
//        return route;
//    }

}
